﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Persistent;
using S4Store.Domain;
using LX.Core.Query;
using S4Store.Service.Valid.CarKpiValid;
using S4Store.Service.Valid.StoreValid;
using S4Store.Domain.CarKpi;
using S4Store.Service.Valid.CarKpiValid;

namespace S4Store.Service
{
    public class CarKpiService : BaseService
    {
        int comfirmstatus = DataConfirmStatus.Confirmed.ToInt();

        public CarKpiMonthConfigSave CarKpiMonthConfigSave { get; set; }

        public IQueryable<CarKpiMonthConfig> GetCarKpiMonthConfigQuery()
        {
            return this.GetQuery<CarKpiMonthConfig>();
        }
        public IQueryable<CarModelMonthConfig> GetCarModelMonthConfigQuery()
        {
            return this.GetQuery<CarModelMonthConfig>();
        }
        public IQueryable<CarKpiDayData> GetCarKpiDayDataQuery()
        {
            return this.GetQuery<CarKpiDayData>();
        }
        public IQueryable<CarModel> GetCarModelQuery()
        {
            return this.GetQuery<CarModel>();
        }

        public IQueryable<CarBrand> GetCarbrandsForStoreMonth(int year, int month,int storeid)
        {
            var query = this.GetQuery<CarModelMonthConfig>();
            var result = from q in query
                         where q.IsConfirmed == true &&
                               q.StoreId == storeid &&
                               q.Month.Year == year &&
                               q.Month.Month == month
                         select q.CarModel.Brand;
            return result.Distinct();
        }

        public IQueryable<CarBrand> GetCarbrandsForStoreMonths(DateTime start, DateTime end, int storeid)
        {
            DateTime s = new DateTime(start.Year, start.Month, 1);
            DateTime e = new DateTime(end.Year, end.Month, 1).MonthLastOrNow();
            e = new DateTime(e.Year, e.Month, e.Day, 23, 59, 59);
            var query = this.GetQuery<CarModelMonthConfig>();
            var result = from q in query
                         where q.IsConfirmed == true &&
                               q.StoreId == storeid &&
                               q.Month>=s &&
                               q.Month<=e
                         select q.CarModel.Brand;
            return result.Distinct();
        }

        public IQueryable<CarModel> GetCarmodelsForStoreMonth(int year, int month, int storeid)
        {
            var query = this.GetQuery<CarModelMonthConfig>();
            var result = from q in query
                         where q.IsConfirmed == true &&
                               q.StoreId == storeid &&
                               q.Month.Year == year &&
                               q.Month.Month == month
                         select q.CarModel;
            return result;
        }


        public IQueryable<CarModel> GetCarmodelsForStoreMonth(int year, int month, int storeid,int carbrandid)
        {
            var query = this.GetQuery<CarModelMonthConfig>();
            var result = from q in query
                         where q.IsConfirmed == true &&
                               q.StoreId == storeid &&
                               q.Month.Year == year &&
                               q.Month.Month == month&&
                               q.CarModel.BrandId==carbrandid
                         select q.CarModel;
            return result;
        }


        public IQueryable<CarModel> GetCarmodelsForStoreMonths(DateTime start, DateTime end, int storeid, int carbrandid)
        {
            DateTime s = new DateTime(start.Year, start.Month, 1);
            DateTime e = new DateTime(end.Year, end.Month, 1).MonthLastOrNow();
            e = new DateTime(e.Year, e.Month, e.Day, 23, 59, 59);
            var query = this.GetQuery<CarModelMonthConfig>();
            var result = from q in query
                         where q.IsConfirmed == true &&
                               q.StoreId == storeid &&
                               q.StoreId == storeid &&
                               q.CarModel.BrandId == carbrandid &&
                               q.Month >= s &&
                               q.Month <= e
                         select q.CarModel;
            return result.Distinct();
        }

        public CarKpiMonthConfig GetCarKpiMonthConfig(int year, int month,int storeid,bool onlycomfirmd=false)
        {
            if (onlycomfirmd)
            {
                CarKpiMonthConfig config = this.GetCarKpiMonthConfigQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid && t.IsConfirmed);
                if (config == null)
                {
                    config = new CarKpiMonthConfig();
                    config.Month = new DateTime(year, month, 1);
                    config.StoreId = storeid;
                }
                return config;
            }
            else
            {
                CarKpiMonthConfig config = this.GetCarKpiMonthConfigQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
                if (config == null )
                {
                    config = new CarKpiMonthConfig();
                    config.Month = new DateTime(year, month, 1);
                    config.StoreId = storeid;
                }
                return config;
            }
        }

        public void SubmitCarKpiMonthConfig(CarKpiMonthConfig config)
        {
            this.Valid(config, CarKpiMonthConfigSave);
            if (config.Id == 0)
            {
                this.GetSet<CarKpiMonthConfig>().Add(config);
            }
            
        }

        public void SubmitCarModelMonthConfig(int year, int month,int storeid,List<CarModelMonthConfig> configs)
        {
            var CarModelMonthConfigs = GetCarModelMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
            foreach (var Carc in CarModelMonthConfigs)
            {
                this.GetSet<CarModelMonthConfig>().Remove(Carc);
            }
            foreach (var config in configs)
            {
                this.GetSet<CarModelMonthConfig>().Add(config);
            }
        }

        public IQueryable<CarModelMonthConfig> GetCarModelMonthConfigs(int year, int month, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                return GetCarModelMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid && t.IsConfirmed);
            }
            else
            {
                return GetCarModelMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
            }
        }

        public CarModelMonthConfig GetCarModelMonthConfig(int year, int month, int storeid,int carmodelid, bool onlycomfirmd = false)
        {
            var config = GetCarModelMonthConfigs(year, month, storeid, onlycomfirmd).FirstOrDefault(t=>t.CarModelId==carmodelid);
            if (config == null)
            {
                config = new CarModelMonthConfig
                {
                     CarModelId=carmodelid,
                      StoreId=storeid,
                       Month=new DateTime(year,month,1),
                        
                };
            }
            return config;
        }

        public IQueryable<CarKpiDayData> GetCarKpiDayDatas(int year, int month, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                return GetCarKpiDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.StoreId == storeid && t.IsConfirmed);
            }
            else
            {
                return GetCarKpiDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.StoreId == storeid);
            }
        }


        public IQueryable<CarKpiDayData> GetCarKpiDayDatas(int year, int month,int day, int storeid)
        {
            return GetCarKpiDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.Date.Day == day && t.StoreId == storeid);
        }

        public void SubmitCarKpiDayDatas(int year, int month, int day, int storeid, List<CarKpiDayData> days)
        {
            var carmodelsinplan = GetCarmodelsForStoreMonth(year, month, storeid).ToList();
            
            if (days.Any(t => carmodelsinplan.All(x => x.Id != t.CarModelId)))
            {
                throw new Exception("提交的数据里包含未做月初计划的车款");
            }
            var datas = GetCarKpiDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.Date.Day == day && t.StoreId == storeid);
            foreach (var data in datas)
            {
                this.GetSet<CarKpiDayData>().Remove(data);
            }
            foreach (var config in days)
            {
                this.GetSet<CarKpiDayData>().Add(config);
            }
        }
    }
}
